#include "client.h"
#include "command.h"

int main(){
    int sockfd;
    init_socket(&sockfd,"8080","127.0.0.1");
    //注册和登录
    char user_name[1024] = {0};
    char cur_path[2048] = {0};
    login_and_sigup(sockfd,user_name);
    // printf("user_name:%s\n",user_name);
    
    // char temp[50] = {0};
    // //拼接路径
    // strcat(temp,"/");
    //将根路径放到cur_path中
    // memcpy(cur_path,temp,strlen(temp));
    memcpy(cur_path,"/",1);

    data msg;
    while(1){
        print_cmd_user(user_name,cur_path);
        memset(&msg,0,sizeof(msg));
        //接收命令的数组
        char buf[1024] = {0}; 
        read(STDIN_FILENO,buf,sizeof(buf));
        //将最后一个位置的字符变成空
        buf[strlen(buf)-1] = '\0';
        bool flag = string_concat(&msg,buf);
        // printf("command:%s\n",msg.command);
        // printf("args:%s\n",msg.args);
        if(!flag){
            printf("命令不合法，请重新输入:\n");
            continue;
        }
        if(strcmp(msg.command,"ls") == 0){
            handle_ls(cur_path,&msg,sockfd);
        }else if(strcmp(msg.command,"pwd") == 0){
            printf("%s\n",cur_path);
        }else if(strcmp(msg.command,"cd") == 0){
             handler_cd(sockfd, msg, cur_path);
        }else if(strcmp(msg.command,"rm") == 0){
            send_remove_message(cur_path,&msg,sockfd);
            recv_remove_message(sockfd,&msg);
        }else if(strcmp(msg.command,"gets") == 0){
            handler_gets(sockfd, msg, cur_path);
        }else if(strcmp(msg.command,"puts") == 0){
            handler_puts(sockfd, msg, cur_path);
        }else if(strcmp(msg.command,"mkdir") == 0){
            handler_mkdir(cur_path,&msg,sockfd);
        }
    }
    close(sockfd);
    return 0;
}

